package com.hellowx.qydcdev.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import com.hellowx.qydcdev.annotaion.RequiredToken;
import com.hellowx.qydcdev.entity.Business;
import com.hellowx.qydcdev.entity.BusinessVO;
import com.hellowx.qydcdev.service.BusinessService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * Created on 2022/1/5.
 *
 * @author Hello WX
 */
@Api(tags = "店铺模块")
@RestController
@RequestMapping("/business")
public class BusinessController {

    @Resource
    private BusinessService businessService;

    @ApiOperation("获取店铺,分页")
    @GetMapping("/page")
    public ResponseEntity<PageInfo<BusinessVO>> getBusinessListByPage(Page<Business> page){
        PageInfo<BusinessVO> businessVOPageInfo = businessService.selectListByPage(page);
        return ResponseEntity.ok(businessVOPageInfo);
    }

    @ApiOperation("查询店铺信息(包含商品信息)")
    @GetMapping("/includeGoodsById")
    public ResponseEntity<BusinessVO> getBusinessIncludeGoodsById(@RequestParam("id") Long businessId){
        BusinessVO businessVO = businessService.getBusinessIncludeGoodsById(businessId);
        return ResponseEntity.ok(businessVO);
    }

    @ApiOperation("通过店铺名称查询店铺,分页")
    @GetMapping("/searchByNameAndPage")
    @RequiredToken
    public ResponseEntity<PageInfo<Business>> getBusinessByNameAndPage(@RequestParam("businessName") String businessName,
                                                     @RequestParam("pageNum") int pageNum,
                                                     @RequestParam("pageSize") int pageSize){
        PageInfo<Business> businessListPage =businessService.listBusinessByNameAndPage(businessName,pageNum,pageSize);
        return ResponseEntity.ok(businessListPage);
    }

    @ApiOperation("通过店铺id获取店铺信息,以及相关联的后台登录账号")
    @GetMapping("/getByidIncludeUser")
    @RequiredToken
    public ResponseEntity<BusinessVO> getBusinessByIdIncludeUser(@RequestParam("businessId") Long businessId){
        BusinessVO businessVO = businessService.getByIdIncludeUser(businessId);
        return ResponseEntity.ok(businessVO);
    }

    @ApiOperation("新增店铺信息")
    @PostMapping("/save")
    @RequiredToken
    public ResponseEntity<Map<String, String>> saveBusiness(@RequestBody Business business){
        String msg = businessService.saveBusiness(business);
        Map<String, String> map = new HashMap<>(2);
        map.put("msg",msg);
        map.put("businessId",business.getId()+"");
        return ResponseEntity.ok(map);
    }
}
